import { createApp } from 'vue';
import './style.css';
import '@/api/interceptor';
import App from './App.vue';
import router from '@/router';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import { createPinia } from 'pinia';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
import formCreate from '@form-create/element-ui';
import VxeUI from 'vxe-pc-ui';
import 'vxe-pc-ui/lib/style.css';
import VxeTable from 'vxe-table';
import 'vxe-table/lib/style.css';

const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

const app = createApp(App);
app.use(pinia);
app.use(router);
app.use(ElementPlus);
app.use(formCreate);
app.use(VxeUI).use(VxeTable);

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}

// api mock相关
const enableMocking = async () => {
  if (process.env.NODE_ENV !== 'development') return;
  const { worker } = await import('@/mocks');
  await worker.start({
    onUnhandledRequest(request, print) {
      // 决定如何对未处理的请求做出反应
      // 如果url不包含/api的内容，返回
      if (request.url.indexOf('/api') === -1) return;
      // 否则就警告
      print.warning();
      console.log(request.url);
    },
  });
};

await enableMocking();
app.mount('#app');
